package com.skycode7.servlet;

import com.skycode7.bean.AdminBean;
import com.skycode7.bean.NewsBean;
import com.skycode7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/newsAdd/jsp")
public class NewsAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String newsTitle = req.getParameter("newsTitle");
        String newsContent = req.getParameter("NewsContent");
        NewsBean newsBean = new NewsBean();//实例化一个新闻对象
        //设置新闻对象属性值
        newsBean.setNewsTitle(newsTitle);
        newsBean.setNewsContent(newsContent);
        newsBean.setNewsTime( (new Date()).toString()  );
        newsBean.setAdminName("admin");
        NewsDao newsDao = new NewsDao();
        AdminBean AdminBean = null;
        int result = newsDao.add(AdminBean);
        if (result > 0){
            //补充知识 跳转到网页news.html中
            resp.sendRedirect("news.html");
        }else{
            req.getRequestDispatcher("WEB-INF/pages/newsAdd.jsp").forward(req,resp);
        }

    }
}
